<?php

/**
 * Controller/ImportController.php
 */

namespace RHX\Controller;

use RHX\Helper\RHDateTime;
use RHX\Model\Object\XRules;

class ImportController extends RHXController {

    public function indexAction() {
	//
    }

    public function loadAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// Ouverture du fichier CSV
	$file	 = fopen('module/RHX/data/bd.csv', 'r');
	//var_dump($file);
	// Entête
	$line	 = fgetcsv($file, 1024, ';');
	$inserts = array();
	while (($line	 = fgetcsv($file, 1024, ';')) !== false) {
	    //var_dump($line);
	    $nom		 = addslashes(utf8_encode($line[2]));
	    $prenom		 = addslashes(utf8_encode($line[3]));
	    $matricule	 = (int) $line[1];
	    $insert		 = "INSERT INTO tmp_employe (matricule, nom, prenom) VALUES ($matricule, '$nom', '$prenom');";
	    $inserts[]	 = $insert;
	}
	fclose($file);

	return array(
	    'inserts' => $inserts,
	);
    }

    /**
     * Interface pour les calculs
     *
     * @return array
     */
    public function calculAction() {
	return array();
    }

    /**
     * Calcul des heures travaillées de toute les plannings Semaine
     *
     * @return array
     */
    public function calculateHeureTravailAction() {
	// POST
	$post		 = $this->getRequest()
		->getPost();
	// Table Planning Semaine
	$tablePlanning	 = new \RHX\Model\Table\PlanningSemaineTable($this->getAdapter());

	// Variables
	$perPage = $post->perPage;
	$page	 = $post->page;
	$offset	 = ($page - 1) * $perPage;

	$plannings = $tablePlanning->select(function(\Zend\Db\Sql\Select $select)use($perPage, $offset) {
		    $select->limit((int) $perPage);
		    $select->offset((int) $offset);
		});

	// Calcul des heures Travaillés
	$id = array();
	foreach ($plannings as $pl) {
	    $travail = new RHDateTime();
	    $travail->setTime('00:00');

	    // Lundi
	    $lundiCode = (int) $pl->lundi_code;
	    if ($lundiCode != XRules::NeTravaillePas || $lundiCode != XRules::Absent || $lundiCode != XRules::CongeSansSolde || $lundiCode != XRules::ArretMaladie1 || $lundiCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->lundi_sortie, $pl->lundi_entree, false);
		if ((int) $pl->lundi_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Mardi
	    $mardiCode = (int) $pl->mardi_code;
	    if ($mardiCode != XRules::NeTravaillePas || $mardiCode != XRules::Absent || $mardiCode != XRules::CongeSansSolde || $mardiCode != XRules::ArretMaladie1 || $mardiCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->mardi_sortie, $pl->mardi_entree, false);
		if ((int) $pl->mardi_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Mercredi
	    $mercrediCode = (int) $pl->mercredi_code;
	    if ($mercrediCode != XRules::NeTravaillePas || $mercrediCode != XRules::Absent || $mercrediCode != XRules::CongeSansSolde || $mercrediCode != XRules::ArretMaladie1 || $mercrediCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->mercredi_sortie, $pl->mercredi_entree, false);
		if ((int) $pl->mercredi_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Jeudi
	    $jeudiCode = (int) $pl->jeudi_code;
	    if ($jeudiCode != XRules::NeTravaillePas || $jeudiCode != XRules::Absent || $jeudiCode != XRules::CongeSansSolde || $jeudiCode != XRules::ArretMaladie1 || $jeudiCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->jeudi_sortie, $pl->jeudi_sortie, false);
		if ((int) $pl->jeudi_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Vendredi
	    $vendrediCode = (int) $pl->vendredi_code;
	    if ($vendrediCode != XRules::NeTravaillePas || $vendrediCode != XRules::Absent || $vendrediCode != XRules::CongeSansSolde || $vendrediCode != XRules::ArretMaladie1 || $vendrediCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->vendredi_sortie, $pl->vendredi_entree, false);
		if ((int) $pl->vendredi_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Samedi
	    $samediCode = (int) $pl->samedi_code;
	    if ($samediCode != XRules::NeTravaillePas || $samediCode != XRules::Absent || $samediCode != XRules::CongeSansSolde || $samediCode != XRules::ArretMaladie1 || $samediCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->samedi_sortie, $pl->samedi_entree, false);
		if ((int) $pl->samedi_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Dimanche
	    $dimancheCode = (int) $pl->dimanche_code;
	    if ($dimancheCode != XRules::NeTravaillePas || $dimancheCode != XRules::Absent || $dimancheCode != XRules::CongeSansSolde || $dimancheCode != XRules::ArretMaladie1 || $dimancheCode != XRules::Fermer) {
		$diff = RHDateTime::diffHours($pl->dimanche_sortie, $pl->dimanche_entree, false);
		if ((int) $pl->dimanche_pause == 1) {
		    $diff = RHDateTime::diffHours($diff, '00:30');
		}
		$travail->addHours($diff, false);
		// echo "<pre>Différence : $diff</pre>";
	    }

	    // Enregistrement dans la base de données
	    $pl->heure_travail	 = $travail->totalHours();
	    $id[]			 = $tablePlanning->save($pl) . ' heures travaillées : ' . $pl->heure_travail;
	}

	// Changement de layout
	$this->layout('layout/ajax');

	return array(
	    'id' => $id,
	);
    }

    /**
     * Comptage du nombre de page des Plannings Semaines
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function getPageCountAction() {
	// Planning Semaine Table
	$tablePlanning	 = new \RHX\Model\Table\PlanningSemaineTable($this->getAdapter());
	$plannings	 = $tablePlanning->getAll();
	$count		 = $plannings->count();

	// POST
	$post = $this->getRequest()
		->getPost();

	$perPage	 = (int) $post->perPage;
	$pageCount	 = (int) ($count / $perPage);
	$mod		 = $count % $perPage;
	if ($mod != 0) {
	    $pageCount++;
	}

	return new \Zend\View\Model\JsonModel(array('pageCount' => $pageCount));
    }

    public function testAction() {
	//
    }

    /**
     * Calcul des heures non planifié par semaine de période
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function heureNonPlanifieAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Calculator
	$calculator = new \RHX\Model\Object\XCalculator($this->getAdapter());

	// POST
	$post = $this->getRequest()
		->getPost();

	// Planning Semaine table
	$tablePlanning	 = new \RHX\Model\Table\PlanningSemaineTable($this->getAdapter());
	$planning	 = $tablePlanning->get($post->id);

	$data = $calculator->getHeureNonPlannifie($planning->matricule, $planning->lundi);

	return new \Zend\View\Model\JsonModel(array(
	    //'week'	 => $week,
	    'data'			 => $data,
	    'heureNonPlanifie'	 => $data['heureNonPlanifie'],
	));
    }

    public function updateMatriculeAction() {
	// table employe RH2
	$tableEmployeRH	 = new \RH2\Model\Table\EmployeTable($this->getAdapter());
	$employes	 = $tableEmployeRH->select(function(\Zend\Db\Sql\Select $select) {
		    $where = new \Zend\Db\Sql\Where();
		    $where->isNull('matricule');
		    $select->where($where);
		});
	// Mise à jour
	$emp		 = array();
	$sql		 = "SELECT MAX(matricule) as 'max' FROM rhx_employe";
	$result		 = $this->getAdapter()
		->query($sql)
		->execute();
	$data		 = $result->current();
	$max	 = (int) $data['max'];
	$done	 = 0;
	$err	 = array();
	//
	foreach ($employes as $e) {
	    $e->matricule	 = ++$max;
	    $arrayCopy	 = array(
		'id'			 => $e->id,
		'matricule'		 => $e->matricule,
		'site_id'		 => $e->site_id,
		'nom1'			 => $e->nom1,
		'nom2'			 => $e->nom2,
		'prenom'		 => $e->prenom,
		'telephone'		 => $e->telephone,
		'email'			 => $e->email,
		'contrat_debut'		 => $e->contrat_debut,
		'contrat_fin'		 => $e->contrat_fin,
		'contrat_type'		 => $e->contrat_type,
		'avenant_id'		 => $e->avenant_id,
		'civil_id'		 => $e->civil_id,
		'situation_id'		 => $e->situation_id,
		'enfant'		 => $e->enfant,
		'naissance'		 => $e->naissance,
		'naissance_lieu'	 => $e->naissance_lieu,
		'naissance_pays'	 => $e->naissance_pays,
		'adresse1'		 => $e->adresse1,
		'cp1'			 => $e->cp1,
		'ville1'		 => $e->ville1,
		'adresse2'		 => $e->adresse2,
		'cp2'			 => $e->cp2,
		'ville2'		 => $e->ville2,
		'sejour_id'		 => $e->sejour_id,
		'sejour_num'		 => $e->sejour_num,
		'sejour_fin'		 => $e->sejour_fin,
		'cpam'			 => $e->cpam,
		'pole_emploi'		 => $e->pole_emploi,
		'mutuelle'		 => $e->mutuelle,
		'mutuelle_date_effet'	 => $e->mutuelle_date_effet,
		'rib'			 => $e->rib,
		'transport_id'		 => $e->transport_id,
		'actif'			 => $e->actif,
	    );
	    try {
		//$tableEmployeRH->update($data, array('id' => (int) $e->id));
		$tableEmployeRH->update($arrayCopy, array('id' => (int) $e->id));
		$done++;
	    }
	    catch (\Exception $ex) {
		$err[] = $ex->getMessage();
		break;
	    }
	    $emp[]		 = $e;
	}
	//$employes->rewind();

	return array(
	    'employes'	 => $emp,
	    'done'		 => $done,
	    'error'		 => $err,
	);
    }

}

?>
